<?php

namespace App\Admin\Controllers;

use App\Admin\Grid\NGrid;
use App\Admin\Tools\Task\TaskFollowUpTool;
use App\Models\BlacklistModel;
use App\Models\ContractedCustomerModel;
use Illuminate\Support\Facades\Auth;
use App\Models\TaskUserRelatedModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid\Displayers\Actions;

class TaskUserRelatedController extends AdminController
{
    public function title(): string
    {
        return '我的任务-任务预览';
    }

    public function grid(): NGrid
    {
        $grid = new NGrid(new TaskUserRelatedModel);
        $grid->model()->leftJoin('task_related', 'task_related.id', 'task_user_related.task_related_id')
            ->where('task_user_id', request()->segment(4))
            ->select([
                'task_user_related.id',
                'task_user_related.status',
                'task_user_related.extends',
                'task_user_id',
                'task_related.name',
                'task_related.gender',
                'task_related.age',
                'task_related.area',
                'task_related.mobile',
                'task_related.wechat',
                'task_related.task_id',
                'task_related.remark',
            ]);

        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->like('task_related-name', '客户姓名');
                $filter->like('task_related-remark', __('l.remark'));
                $filter->equal('task_user_related-status', '状态')->select(__('task.followStatusArr'));
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->like('task_related-mobile', '手机号');
                $filter->like('task_related-wechat', '微信');
//                $filter->equal('task_user_related-status', '微信')->select(__('task.followStatusArr'));
            });
        });

        if (Auth::user()->roles->first()->slug == 'Administrator') $grid->column('id', 'ID')->sortable();
        $grid->column('name', '客户姓名')->style('max-width:5em;word-break:break-all;')->display(function () {
            $mobile = $this->mobile;
            $wechat = $this->wechat;
            if (!is_null($mobile)) {
                $blacklist = BlacklistModel::query()->where(function ($query) use ($wechat, $mobile) {
                    if ($mobile != '' && $wechat != '') {
                        return $query->orWhere('wechat', $wechat)
                            ->orWhere('mobile', $mobile);
                    } else if ($mobile == '' && $wechat != '') {
                        return $query->where('wechat', $wechat);
                    } else if ($mobile != '' && $wechat == '') {
                        return $query->where('mobile', $mobile);
                    }
                })->first();
                $contract = ContractedCustomerModel::query()->where(function ($query) use ($mobile) {
                    return $query->orWhere('male_mobile', $mobile)
                        ->orWhere('female_mobile', $mobile);
                })->first();
                if (!is_null($blacklist) && !is_null($contract)) return $this->name . '<br /><span class="label " style="background-color: #1a2226;color:white">黑</span> &nbsp;<span class="label label-info">签</span>';
                else if (!is_null($blacklist) && is_null($contract)) return $this->name . '<br /><span class="label " style="background-color: #1a2226;color:white">黑</span>';
                else if (is_null($blacklist) && !is_null($contract)) return $this->name . '<br /><span class="label label-info">签</span>';
                else return $this->name;
            } else return $this->name;

        });
        $grid->column('gender', __('l.gender'))->display(function () {
            return $this->gender ? '男' : '女';
        });
        $grid->column('age', __('l.age'));
        $grid->column('area', __('task.area'));
        $grid->column('mobile', __('l.mobile'))->style('max-width:3em;word-break:break-all;')->display(function () {
            return is_null($this->mobile) ? '' : maskMobile($this->mobile);
        });
        $grid->column('wechat', __('l.wechat'))->style('max-width:4em;word-break:break-all;');
        $grid->column('remark', __('l.remark'))->style('max-width:6em;word-break:break-all;')->display(function () {
            if (mb_strlen($this->remark) < 16) return $this->remark;
            else return view('tooltip ', [
                'data' => $this->remark
            ]);
        });
        $grid->column('status', __('l.status'))->using(__('task.followStatusArr'));

        $grid->actions(function (Actions $action) {
            $action->disableEdit();
            $action->disableView();
            $action->disableDelete();

            if ($action->row->mobile !== '' || $action->row->wechat !== '') {
                $mobile = $action->row->mobile;
                $wechat = $action->row->wechat;
                $blacklist = BlacklistModel::query()->where(function ($query) use ($wechat, $mobile) {
                    if ($mobile != '' && $wechat != '') {
                        return $query->orWhere('wechat', $wechat)
                            ->orWhere('mobile', $mobile);
                    } else if ($mobile == '' && $wechat != '') {
                        return $query->where('wechat', $wechat);
                    } else if ($mobile != '' && $wechat == '') {
                        return $query->where('mobile', $mobile);
                    }
                })->first();

                if (in_array((int)$action->row->status, [0, 1, 3]) && is_null($blacklist)) $action->append(new TaskFollowUpTool(
                    $action->row->id,
                    $action->row->mobile,
                    $action->row->wechat,
                    $action->row->extends,
                    $action->row->remark
                ));
            }
        });
        $grid->disableCreateButton();
        return $grid;
    }
}
